package com.daohang361.imqq.ui.headtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.csu.client.module.vo.Group;
import com.csu.client.module.vo.MsgUser;
import com.daohang361.imqq.resourse.ResourcesManagement;


public class EIMTree extends Box implements MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3609266265530497720L;
	static ImageIcon collapsedIamge=ResourcesManagement.getImageIcon("ui/collapsed.gif", 16, 16);
	static ImageIcon expandedIamge=ResourcesManagement.getImageIcon("ui/expanded.gif", 16, 16);
	
	private Group dataResoure;
	private JLabel treeInfo;
	private boolean isexp=false;
	private List<EIMTreeNode> treeOnlineNodes=new ArrayList<EIMTreeNode>();
	Box userBox=Box.createVerticalBox();//
	private List<EIMTreeNode> treeOffNodes=new ArrayList<EIMTreeNode>();
	
	public EIMTree(){
		super(BoxLayout.Y_AXIS);
		priantTree();
	}
	
	public EIMTree(Group dataResoure) {
		super(BoxLayout.Y_AXIS);
		this.dataResoure = dataResoure;
		priantTree();
		
	}
	
	public void priantTree(){
		if(dataResoure==null){
			treeInfo=new JLabel("未命名[0/0]");
			treeInfo.setIcon(collapsedIamge);
			Box box=Box.createHorizontalBox();
			box.add(treeInfo,BorderLayout.WEST);
			box.add(new JLabel("               "));
			this.add(box,BorderLayout.EAST);
		}else{
			initUsers();
			treeInfo=new JLabel(dataResoure.getGroupName());
			treeInfo.setIcon(collapsedIamge);
			Box box=Box.createHorizontalBox();
			box.add(treeInfo,BorderLayout.WEST);
			int onlineNum=treeOnlineNodes.size();
			int allNum=treeOffNodes.size()+onlineNum;
			box.add(new JLabel("["+onlineNum+"/"+allNum+"]"));
			String s=dataResoure.getGroupName()+"["+onlineNum+"/"+allNum+"]";
			String sformate=formate(s);
			box.add(new JLabel(sformate));
			this.add(box,BorderLayout.WEST);
			
			for(int i=0;i<treeOnlineNodes.size();i++){
				userBox.add(treeOnlineNodes.get(i));
				this.add(userBox);
			}
			for(int i=0;i<treeOffNodes.size();i++){
				userBox.add(treeOffNodes.get(i));
				this.add(userBox);
			}
		}
		treeInfo.addMouseListener(new LaleMouseAdapter());
	}
public String formate(String s){
	int length1 = s.getBytes().length;
	int length2 = s.length();
	int length=length2+(length1-length2)/2;
	StringBuilder temps=new StringBuilder();
	for(int i=0;i<40-length;i++)
	{
		temps.append(".");
	}
	return temps.toString();
}
	public void initUsers(){
		for(int i=0;i<dataResoure.getGrops().size();i++){
			MsgUser user=dataResoure.getGrops().get(i);
			EIMTreeNode treeNode=new EIMTreeNode(user);
			if(user.isOnline()){
				treeOnlineNodes.add(treeNode);
			}else{
				treeOffNodes.add(treeNode);
			}
			
		}
	}
	public void mouseClicked(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}
	
	class LaleMouseAdapter extends MouseAdapter{
		
		public void mouseEntered(final MouseEvent e) {
			treeInfo.setForeground(Color.black);
    	}
		
    	public void mouseClicked(final MouseEvent e) {
    		if(e.getClickCount()==1||e.getClickCount()==2){
    			System.out.print("双击");
    			if(isexp){
    				for(int i=0;i<treeOnlineNodes.size();i++){
    					treeOnlineNodes.get(i).setVisible(false);
    		
        			}
    				for(int i=0;i<treeOffNodes.size();i++){
    					treeOffNodes.get(i).setVisible(false);
        			}
    				treeInfo.setIcon(collapsedIamge);
    				isexp=false;
    			}else{
    				for(int i=0;i<treeOnlineNodes.size();i++){
    					treeOnlineNodes.get(i).setVisible(true);
        			}
    				for(int i=0;i<treeOffNodes.size();i++){
    					treeOffNodes.get(i).setVisible(true);
        			}
    				treeInfo.setIcon(expandedIamge);
    				isexp=true;
    			}
    			
    		}
    	}
    	public void mouseExited(final MouseEvent e) {
    		treeInfo.setBackground(Color.white);
    	}
	}

	public List<EIMTreeNode> getTreeOnlineNodes() {
		return treeOnlineNodes;
	}

	public void setTreeOnlineNodes(List<EIMTreeNode> treeOnlineNodes) {
		this.treeOnlineNodes = treeOnlineNodes;
	}


	public List<EIMTreeNode> getTreeOffNodes() {
		return treeOffNodes;
	}

	public void setTreeOffNodes(List<EIMTreeNode> treeOffNodes) {
		this.treeOffNodes = treeOffNodes;
	}

}
